package ui;

import java.awt.Dimension;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.util.ArrayList;
import java.util.List;

import javax.swing.AbstractAction;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

public class CanvasDialog extends JDialog {
	public static final int INPUT_MODE = 0;
	public static final int DISPLAY_MODE = 1;

	private static final int WINDOW_WIDTH = 500;
	private static final int WINDOW_HEIGHT = 500;

	public static final int OK = 0;
	public static final int CANCEL = 1;
	private PointCanvas canvas;
	private JButton okButton;
	private JButton cancelButton;

	private List<Point> points = null;
	private Point[] nearestPointPair = new Point[2];
	private int returnStatus = CANCEL;
	private int mode = INPUT_MODE;

	public CanvasDialog(JFrame owner, int mode) {
		super(owner, true);
		this.mode = mode;
		this.setTitle("Point Canvas");
		int startX = (Toolkit.getDefaultToolkit().getScreenSize().width - WINDOW_WIDTH) / 2;
		int startY = (Toolkit.getDefaultToolkit().getScreenSize().height - WINDOW_HEIGHT) / 2;
		this.setBounds(startX, startY, WINDOW_WIDTH, WINDOW_HEIGHT);

		initComponents();
	}

	private void initComponents() {
		canvas = null;
		if (mode == INPUT_MODE) {
			canvas = new PointCanvas(true);
		} else {
			canvas = new PointCanvas(false);
		}
		JScrollPane pane = new JScrollPane();
		pane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
		pane
				.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
		pane.getViewport().add(canvas);
		this.getContentPane().add("Center", pane);

		JPanel buttonPane = new JPanel();
		buttonPane.setLayout(new BoxLayout(buttonPane, BoxLayout.X_AXIS));
		okButton = new JButton("OK");
		okButton.setPreferredSize(new Dimension(80, 25));
		okButton.addActionListener(new AbstractAction() {
			public void actionPerformed(ActionEvent arg0) {
				points = canvas.getPoints();
				returnStatus = OK;
				dispose();
			}
		});
		cancelButton = new JButton("Cancel");
		cancelButton.addActionListener(new AbstractAction() {
			public void actionPerformed(ActionEvent arg0) {
				points = new ArrayList<Point>();
				returnStatus = CANCEL;
				dispose();
			}
		});
		cancelButton.setPreferredSize(new Dimension(80, 25));
		buttonPane.setPreferredSize(new Dimension(500, 40));
		buttonPane.add(Box.createHorizontalGlue());
		buttonPane.add(okButton);
		buttonPane.add(Box.createHorizontalStrut(10));
		buttonPane.add(cancelButton);
		buttonPane.add(Box.createHorizontalStrut(20));
		this.getContentPane().add("South", buttonPane);
	}

	public List<Point> getPoints() {
		if (points == null) {
			points = new ArrayList<Point>();
		}
		return points;
	}

	public void setPoints(List<Point> points) {
		this.points = points;
		this.canvas.setPoints(points);
	}

	public void setNearestPointPair(Point[] pts) {
		if (pts != null && pts.length == 2 && pts[0] != null && pts[1] != null) {
			this.nearestPointPair[0] = pts[0];
			this.nearestPointPair[1] = pts[1];
			this.canvas.setNearestPointPair(pts);
		}
	}

	public int getReturnStatus() {
		return returnStatus;
	}
}
